package com.aacar.interceptor;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.aacar.controller.ValidController;
import com.aacar.dto.Header;
import com.aacar.service.TokenService;
import com.aacar.utils.ErrorCodeConsField;
import com.aacar.utils.Pphconfig;

@Aspect 
@Component 
@Order(3)
public class MemLoginKeyValidAspect extends ValidController{
	
	private Log logger = LogFactory.getLog(MemLoginKeyValidAspect.class);
	
	@Autowired
	private  HttpServletRequest request;
	
	@Resource
	private TokenService tokenService;
	
	 //标注该方法体为后置通知，当目标方法执行成功后执行该方法体 
	@Before(value="execution(* com.aacar.controller..*.*(..)) && @annotation(MemLoginKeyValid)")
    public void loginValid(JoinPoint joinPoint) throws Exception{ 
		logger.info("#### MemLoginKeyValid  LoginKeyValid before ################");
		Header mobileHead = getMobileHead(request);
		if(mobileHead==null){
			//请求头信息为空
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10212, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10212),mobileHead.getService());
		}
		if(null == mobileHead.getUserId() || "".equals(mobileHead.getUserId()) || "null".equals(mobileHead.getUserId().toLowerCase())){
			//请求头信息(会员ID为空)
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10213, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10213),mobileHead.getService());
		}
		String token = mobileHead.getToken();
		if("".equals(token)){
			//请求头信息(没有token需要登录)
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10214, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10214),mobileHead.getService());
		}
		//校验token有效无效
		String tv = tokenService.validatorToken(token);
		if(null == tv) {
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10218, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10218),mobileHead.getService());
		}

		try{
			Long.valueOf(mobileHead.getUserId());
		}catch (Exception e){
			e.printStackTrace();
			//请求头信息(会员ID不存在)
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10215, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10215));
		}
		/*Member member = memberService.getMember(mobileHead.getMemberId());
		logger.info("#### member :"+JSONObject.toJSONString(member));
		if(member!=null){
			if(member.getLoginKey().equals(mobileHead.getToken())){
				if("2".equals(member.getStatus())){
					//该账户禁用
					throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10208, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10208));
			    }
				logger.info("#### MemLoginKeyValid LoginKeyValid success!");
			}else{
				//请求头信息(登录key错误)
				throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10216, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10216));
			}
		}else{
			//请求头信息(会员ID不存在)
			throw new TranFailException(ErrorCodeConsField.ERROR_MSG_10215, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10215));
		}*/
		logger.info("####  LoginKeyValid after ################");
	}
}

